package com.qf.service.impl;

import com.qf.dao.repository.UserRepository;
import com.qf.pojo.entity.LiveUser;
import com.qf.pojo.repVo.BaseResp;
import com.qf.pojo.repVo.UserRepVo;
import com.qf.pojo.reqVo.UserReqVo;
import com.qf.service.UserService;
import com.qf.utils.JwtUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepository;



    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public BaseResp validataEmail(String email) {
        if (email==null){
            return new BaseResp().Error();
        }
        LiveUser byEmail = userRepository.findByEmail(email);
        if (byEmail!=null){
            return new BaseResp(0,"邮箱已被占用！",null,null);
        }
        return new BaseResp(1,"邮箱可用！",null,null);
    }

    @Override
    public BaseResp validataloginName(UserReqVo userReqVo) {
        if (userReqVo.getLoginName()==null){
            return new BaseResp().Error();
        }
        LiveUser byEmail = userRepository.findByLoginName(userReqVo.getLoginName());
        if (byEmail!=null){
            return new BaseResp(0,"登录名被占用！",null,null);
        }
        return new BaseResp(1,"登录名可用！",null,null);
    }

    @Override
    public BaseResp getCode(String email) {
        if (email==null){
            return new BaseResp().Error();
        }
        LiveUser byEmail = userRepository.findByEmail(email);
        if (byEmail!=null){
            return new BaseResp(0,"邮箱已被占用！",null,null);
        }
        //准备将 收件人以及 验证码通过异步通信，发送给rabbitmq，进行处理
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0;i<6;i++){
            stringBuffer.append(new Random().nextInt(10));
        }
        String code =stringBuffer.toString();

        //1.声明map
        Map map=new HashMap();
        map.put("email",email);
        map.put("code",code);
        //通过rabbitmq进行异步通信
        rabbitTemplate.convertAndSend("","qflive-email",map);
        return new BaseResp(1,"发送成功",null,null);
    }

    @Override
    public BaseResp registry(UserReqVo userReqVo) {
        String email = userReqVo.getEmail();
        if (email==null){
            return new BaseResp().Error();
        }
        LiveUser byEmail = userRepository.findByEmail(email);
        if (byEmail!=null){
            return new BaseResp(0,"邮箱已被占用！",null,null);
        }
        if (userReqVo.getLoginName()==null){
            return new BaseResp().Error();
        }
        LiveUser liveUser = userRepository.findByLoginName(userReqVo.getLoginName());
        if (liveUser!=null){
            return new BaseResp(0,"登录名被占用！",null,null);
        }

        //验证码的比对
        Boolean aBoolean = redisTemplate.hasKey(email);
        if (!aBoolean){
            return new BaseResp(0,"验证码已过期!",null,null);
        }
        Object o = redisTemplate.opsForValue().get(email);
        if (!userReqVo.getCode().equals(o.toString())){
            return new BaseResp(0,"验证码输入错误!",null,null);
        }
        //存储到数据库中
        LiveUser liveUserSave = new LiveUser();
        BeanUtils.copyProperties(userReqVo,liveUserSave);
        userRepository.save(liveUserSave);

        return new BaseResp(1,"注册成功！",null,null);
    }

    @Override
    public BaseResp login(UserReqVo userReqVo) {
        if (userReqVo.getLoginName()==null){
            return new BaseResp(0,"用户名不能为空！",null,null);
        }
        if (userReqVo.getPassword()==null){
            return new BaseResp(0,"密码不能为空！",null,null);
        }
        //使用用户名查询出对象
        LiveUser byLoginName = userRepository.findByLoginName(userReqVo.getLoginName());
        if (byLoginName==null){
            return new BaseResp(0,"没有该用户！",null,null);
        }
        //比对密码
        if (!byLoginName.getPassword().equals(userReqVo.getPassword())){
            return new BaseResp(0,"密码输入错误！",null,null);
        }
        //登录成功 我们需要生成token串返回到前端
        Map map = new HashMap<>();
        map.put("name",byLoginName.getLoginName());
        map.put("id",byLoginName.getUserId());
        String token = jwtUtils.signToken(map);

        return new BaseResp(1,"登录成功!",token,null);
    }

    @Override
    public BaseResp findUserByToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            return new BaseResp(0,"token为null",null,null);
        }
        //使用jwt解析Token

        Integer verfiy = jwtUtils.verfiy(token);
        if (verfiy==0){
            return new BaseResp(0,"token验证失败！",null,null);
        }
        //从数据库查询出用户的数据
        Optional<LiveUser> byId = userRepository.findById(verfiy);
        if (!byId.isPresent()){
            return new BaseResp(0,"没有该用户！",null,null);
        }
        UserRepVo userRepVo = new UserRepVo();
        BeanUtils.copyProperties(byId.get(),userRepVo);
        return new BaseResp(1,"验证成功！",userRepVo,null);
    }
}
